package cn.wolfcode.luowowo2.website.web.controller;

import cn.wolfcode.luowowo2.search.query.SearchQueryObject;
import cn.wolfcode.luowowo2.search.service.*;
import cn.wolfcode.luowowo2.search.template.DestinationTemplate;
import cn.wolfcode.luowowo2.search.template.StrategyTemplate;
import cn.wolfcode.luowowo2.search.template.TravelTemplate;
import cn.wolfcode.luowowo2.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo2.search.vo.SearchResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Albert on 2019/7/16.
 */
@Controller
public class SearchController {
    @Reference
    private IDestinationSearchService destinationSearchService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private IStrategySearchService strategySearchService;

    @Reference
    private IUserInfoSearchService userInfoSearchService;

    @Reference
    private ISearchService searchService;

    @RequestMapping("/q")
    public String query(Model model, @ModelAttribute("qo") SearchQueryObject qo){
        //-1 全部

        //0 目的地

        //1 攻略

        //2 游记

        //3 用户

        switch (qo.getType()){
            case 0:
                return destSearch(model,qo);
            case 1:
                return strategySearch(model,qo);
            case 2:
                return travelSearch(model,qo);
            case 3:
                return userSearch(model,qo);
            default:
                return allSearch(model,qo);
        }

    }

    private String allSearch(Model model, SearchQueryObject qo) {
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "names", "info");

        Page<StrategyTemplate> strategies = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");

        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");

        SearchResult data = new SearchResult();
        data.setDests(dests.getContent());
        data.setStrategys(strategies.getContent());
        data.setTravels(travels.getContent());
        data.setUsers(users.getContent());
        data.setTotal(dests.getTotalElements() + strategies.getTotalElements() + travels.getTotalElements() + users.getTotalElements());
        model.addAttribute("data",data);
        return "index/searchAll";
    }

    private String userSearch(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",users);
        return "index/searchUser";
    }

    private String travelSearch(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",travels);
        return "index/searchTravel";
    }

    private String strategySearch(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> strategies = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page",strategies);
        return "index/searchStrategy";
    }

    private String destSearch(Model model, SearchQueryObject qo) {
        //根据关键字查询目的地对象,存在则查询该目的地下的游记,攻略

        DestinationTemplate dest = destinationSearchService.queryDestByName(qo.getKeyword());
        model.addAttribute("dest",dest);

        SearchResult data = new SearchResult();

        if (dest!=null) {
            //qo.setPageSize(5);
            qo.setDestId(dest.getId());
            Page<TravelTemplate> travels =travelSearchService.query(qo);
            Page<StrategyTemplate> strategies = strategySearchService.queryStrategy(qo);
            Page<UserInfoTemplate> users = userInfoSearchService.query(qo);
            data.setTravels(travels.getContent());
            data.setStrategys(strategies.getContent());
            data.setUsers(users.getContent());
            data.setTotal(travels.getTotalElements() + strategies.getTotalElements() + users.getTotalElements());
        }
        model.addAttribute("data",data);

        return "index/searchDest";
    }
}
